/*
 * Created on 2005-6-17
 *
 * Copyright (c) 2004-2005 Hewlett-Packard Company and Coscon Limited.
 * All Rights Reserved.
 * 
 */
package com.maxjay.common.exception;

import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.maxjay.common.exception.processor.SQLExceptionProcessor;

/**
 * <p>
 * Title: 框架
 * </p>
 * <p>
 * Description: 记录异常信息
 * </p>
 * <p>
 * Copyright: Copyright (c) 2005
 * </p>
 * <p>
 * Company: 公司
 * </p>
 * 
 * @author 郑美旺
 */
public class GeneException extends RuntimeException {
	private static Log log = LogFactory.getLog(GeneException.class);

	private static final long serialVersionUID = 1L;

	private int errCode;

	private String strErrorMessage;

	private String strErrorMessagePattern;

	private int level;

	public int getLevel() {
		return level;
	}

	public static GeneException getGeneException(SQLException sqle) {
		int errCde = SQLExceptionProcessor.getInstance().getGeneExceptionErrorCode(sqle);
		if (errCde == ERR.DB_ERROR)
			return getGeneException(ERR.FW_UNDEFINED_ORA_ERR, new String[] { sqle.getErrorCode()
					+ "：" + sqle.getMessage() });
		else
			return getGeneException(errCde);
	}

	public static GeneException getGeneException(SQLException sqle,String lstPattern) {
		int errCde = SQLExceptionProcessor.getInstance().getGeneExceptionErrorCode(sqle);
		if (errCde == ERR.DB_ERROR)
			return getGeneException(ERR.FW_UNDEFINED_ORA_ERR, new String[] { sqle.getErrorCode()
					+ "：" + sqle.getMessage() });
		else
			return getGeneException(errCde,new String[]{lstPattern}, sqle);
	}
	
	public static GeneException getGeneException(int errCode) {
		return getGeneException(errCode, new String[]{null});
	}
	
	public static GeneException getGeneException(int errCode, String lstPattern) {
		return getGeneException(errCode, new String[]{lstPattern});
	}

	public static GeneException getGeneException(int errCode, String[] lstPattern) {

		String strErrorMessage = ErrorMessage.getErrorMessage(errCode, lstPattern);
		int errLevel = ErrorMessage.getErrorLevel(errCode);
		if (strErrorMessage == null) {
			return new GeneException(-8, "无效的错误代码定义！请检查ErrorCode.properties文件内容！",
					ErrorLevel.ERROR, null);
		}

		StringBuffer sbPattern = new StringBuffer("");
		if (lstPattern != null) {
			for (int i = 0; i < lstPattern.length; i++) {
				if (i > 0) {
					sbPattern.append(new String(";"));
				}
				sbPattern.append(lstPattern[i]);
			}
		}

		return new GeneException(errCode, strErrorMessage, errLevel, sbPattern.toString());
	}
	
	public static GeneException getGeneException(int errCode, Throwable innerException) {
		return GeneException.getGeneException(errCode,
				new String[] { null }, innerException);
	}
	
	public static GeneException getGeneException(int errCode, String lstPattern,
			Throwable innerException) {
		return GeneException.getGeneException(errCode,
				new String[] { lstPattern }, innerException);
	}

	public static GeneException getGeneException(int errCode, String[] lstPattern,
			Throwable innerException) {

		String strErrorMessage = ErrorMessage.getErrorMessage(errCode, lstPattern);

		int errLevel = ErrorMessage.getErrorLevel(errCode);

		if (strErrorMessage == null) {
			return new GeneException(-8, "无效的错误代码定义！请检查ErrorCode.properties文件内容！",
					ErrorLevel.ERROR, null);
		}

		StringBuffer sbPattern = new StringBuffer("");

		if (lstPattern != null) {
			for (int i = 0; i < lstPattern.length; i++) {
				if (i > 0) {
					sbPattern.append(new String(";"));
				}
				sbPattern.append(lstPattern[i]);
			}
		}
		return new GeneException(errCode, strErrorMessage, errLevel, sbPattern.toString(),
				innerException);
	}

	/**
	 * 检查是否定义了错误级别
	 * 
	 * @param errorMessage
	 *            错误消息
	 * @return boolean 如果定义了错误级别，则返回true，否则返回false
	 *
	private static boolean checkLevle(String errorMessage) {
		if (errorMessage.length() >= 2) {
			String levelCode = errorMessage.substring(0, 1);
			String delimiter = errorMessage.substring(1, 2);

			try {
				int level = Integer.parseInt(levelCode);

				if (level >= 0 && level <= 5 && delimiter.equals(ErrorMessage.DELIMITER)) {
					return true;
				}
			} catch (NumberFormatException nfe) {
			}
		}

		return false;
	}
	*/
	
	protected GeneException(int errCode, String errMessage, int errLevel, String errMessagePattern) {
		super(errMessage);
		this.errCode = errCode;
		this.strErrorMessage = errMessage;
		this.strErrorMessagePattern = errMessagePattern;
		this.level = errLevel;
	}

	protected GeneException(int errCode, String errMessage, int errLevel, String errMessagePattern,
			Throwable innerException) {
		super(errMessage, innerException);
		this.errCode = errCode;
		this.strErrorMessage = errMessage;
		this.strErrorMessagePattern = errMessagePattern;
		this.level = errLevel;
	}

	public int getErrCode() {
		return errCode;
	}

	public static void main(String[] args) {
		GeneException e = getGeneException(ERR.DB_ERROR);
		log.debug(e.getMessage());
	}

	public String getStrErrorMessage() {
		return strErrorMessage;
	}

	public void setStrErrorMessage(String strErrorMessage) {
		this.strErrorMessage = strErrorMessage;
	}

	public String getStrErrorMessagePattern() {
		return strErrorMessagePattern;
	}

	public void setStrErrorMessagePattern(String strErrorMessagePattern) {
		this.strErrorMessagePattern = strErrorMessagePattern;
	}
}